package com.xyy.saas.payment.service.auth.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xyy.saas.payment.enums.Role;
import com.xyy.saas.payment.manage.cores.vo.ResultPageVo;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import com.xyy.saas.payment.dao.model.PayUserPo;
import com.xyy.saas.payment.pojo.bo.PayUserEditBo;
import com.xyy.saas.payment.pojo.vo.PayUserVo;
import com.xyy.saas.payment.service.MenuPermissionService;
import com.xyy.saas.payment.service.PayUserService;
import com.xyy.saas.payment.service.auth.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhangwenxiong
 * @Date 2020/11/5 14:32
 * @Version 1.0
 */
@Service(value = "manageUserService")
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private PayUserService payUserService;

    @Autowired
    private MenuPermissionService menuPermissionService;

    private final FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMdd HH:mm:ss");

    @Transactional
    @Override
    public ResultVO editUser(String json) {
        ResultVO result = new ResultVO();
        try {

            if (StringUtils.isEmpty(json)) {
                result.setCode("1");
                result.setMsg("参数为空");
            } else {
                PayUserEditBo editBo = JSONObject.parseObject(json, PayUserEditBo.class);
                PayUserPo userPo = new PayUserPo();
                //先判断是否是需要删除的
                if (null != editBo.getIsDelete() && 1 == editBo.getIsDelete()) {
                    payUserService.deleteUser(editBo.getUserAccount());
                    menuPermissionService.deleteMenu(editBo.getUserAccount());
                } else {
                    userPo.setUserAccount(editBo.getUserAccount());
                    userPo.setActive(editBo.getActive());
                    userPo.setDepart(editBo.getDepart());
                    userPo.setUserName(editBo.getUserName());
                    payUserService.updateUser(userPo);
                }
                result.setCode("0");
                result.setMsg("成功");
            }
        }catch (Exception e){
            log.error("编辑用户信息异常:{}",e);
            result.setCode("1");
            result.setMsg("编辑异常,请联系管理员");
        }
        return result;
    }

    @Override
    public ResultVO addUser(String json) {
        ResultVO result = new ResultVO();
        try {

            if (StringUtils.isEmpty(json)) {
                result.setCode("1");
                result.setMsg("参数为空");
            } else {
                PayUserEditBo editBo = JSONObject.parseObject(json, PayUserEditBo.class);
                if (StringUtils.isEmpty(editBo.getUserAccount())){
                    result.setCode("1");
                    result.setMsg("账号不能为空");
                    return result;
                }

                //判断账号是否存在
                PayUserPo exitPo = payUserService.getUserByAccount(editBo.getUserAccount());
                if (null != exitPo){
                    result.setCode("1");
                    result.setMsg("账号已存在");
                    return result;
                }

                PayUserPo userPo = new PayUserPo();
                userPo.setUserAccount(editBo.getUserAccount());
                userPo.setActive(editBo.getActive());
                userPo.setDepart(editBo.getDepart());
                userPo.setUserName(editBo.getUserName());
                userPo.setRole(Role.NORMAL.getCode());
                payUserService.addUser(userPo);
                result.setCode("0");
                result.setMsg("成功");
            }
        }catch (Exception e){
            log.error("新增用户信息异常:{}",e);
            result.setMsg("1");
            result.setMsg("新增异常,请联系管理员");
        }
        return result;
    }

    @Override
    public ResultPageVo<PayUserVo> getUsers(String json) {
        ResultPageVo pageVo = null;
        try {
            if (StringUtils.isEmpty(json)){
                pageVo = ResultPageVo.createFail("参数为空");
            }else {
                //校验是否有分页参数
                JSONObject jo = JSON.parseObject(json);
                if (null == jo.get("page") || null == jo.get("pageSize")){
                    pageVo = ResultPageVo.createFail("分页数据为空");
                }else {
                    //计算起始位置
                    Integer page = Integer.valueOf(jo.get("page").toString());
                    Integer pageSize = Integer.valueOf(jo.get("pageSize").toString());
                    if (0 == pageSize){
                        pageVo = ResultPageVo.createFail("每页数不能为0");
                        return pageVo;
                    }
                    jo.put("offset",(page-1)*pageSize);

                    //获取总数
                    Long total = payUserService.getTotal(jo);
                    List<PayUserPo> pos = payUserService.getUser(jo);
                    List<PayUserVo> vos = convertPoToVo(pos);
                    pageVo = ResultPageVo.createSuccess(page,pageSize,total,vos);
                    pageVo.setMsg("成功");
                }
            }
        }catch (Exception e){
            log.error("获取用户信息异常:{}",e);
            pageVo = ResultPageVo.createFail("查询异常,请联系管理员");
        }
        return pageVo;
    }

    private List<PayUserVo> convertPoToVo(List<PayUserPo> pos){
        List<PayUserVo> vos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(pos)){
            pos.forEach(po -> {
                PayUserVo vo = new PayUserVo();
                vo.setUserName(po.getUserName());
                vo.setUserAccount(po.getUserAccount());
                vo.setDepart(po.getDepart());
                vo.setCreateTime(fdf.format(po.getCreateTime()));
                vo.setLastLoginTime(fdf.format(po.getLastLoginTime()));
                vo.setActive(po.getActive());
                vos.add(vo);
            });
        }
        return vos;
    }


}
